package com.hsmpay.yl8583.service;

import org.apache.log4j.Logger;
import com.hsmpay.yl8583.utils.PackageUtil;
import com.hsmpay.utils.WriteFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SendSocket {
	static Logger log = Logger.getLogger(SendSocket.class);
	public Socket socket = null;
	public InputStream inStream = null;
	public OutputStream outStream = null;
	public boolean ConnFlag = false;
	public boolean SendFlag = false;
	String ipAdd="203.100.81.240";//北京安心易百测试平台
	String IPort="9002";
	String itimeout="60000";
//	public static String debug = ConConfig.getAttribute("ANXINYB_DAISHOU_DEBUG_STATUS");
//	static{
//		if("1".equals(debug)){
//			ipAdd="192.168.20.10";
//			IPort="9002";
//		}
//	}
	
 public WriteFile wf = null;
	
//	public SendSocket(){
//		wf.log("ipAdd:"+ipAdd,"调试");
//		wf.log("IPort:"+IPort,"调试");
//		wf.log("itimeout:"+itimeout,"调试");
//		Connetor(ipAdd, Integer.parseInt(IPort), Integer.parseInt(itimeout)*1000);
//	}
	
	
	
	public SendSocket(String ipAdd, String IPort, String itimeout,WriteFile wf){
		this.ipAdd = ipAdd;
		this.IPort = IPort;
		this.itimeout = itimeout;
		this.wf = wf;
		wf.log("ipAdd:"+ipAdd,"调试");
		wf.log("IPort:"+IPort,"调试");
		wf.log("itimeout:"+itimeout,"调试");
		Connetor(ipAdd, Integer.parseInt(IPort), Integer.parseInt(itimeout)*1000);
	}
	
	public Boolean Connetor(String ipAdd, int IPort, int itimeout) {
		Boolean reConn=false;
		try {
			wf.log("创建socket连接...","调试");
			socket = new Socket(ipAdd, IPort);
			inStream = socket.getInputStream();
			outStream = socket.getOutputStream();
			socket.setSoTimeout(itimeout);
			ConnFlag = true;
			reConn=true;
		} catch (Exception e) {
			e.printStackTrace();
			ConnFlag = false;
			reConn=false;
		}
		return reConn;
	}

	public byte[] ReadData() {
		byte[] buf = null;
		byte[] f=new byte[2];
		try {
			inStream.read(f);
			int size=Integer.parseInt(PackageUtil.byte2hex(f), 16);
			log.debug("size:"+size);
			buf=new byte[size];
			inStream.read(buf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buf;
	}

	public void SendData(byte[] SendStr) {
		try {
			if (ConnFlag) {
				//Send.print(SendStr);
				outStream.write(SendStr);
				outStream.flush();
				log.debug("Send OK");
				SendFlag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			SendFlag = false;
		}
	}
	/**
	 * 通信检测(0000)
	 * 
	 * @return 是否正常
	 */
	public String detection(byte[] sendStr)
	{
		byte[] response=null;
		String retStr="";
		if(ConnFlag)
		{
			SendData(sendStr);
			if(ConnFlag)
			{
				response=ReadData();
				if(response==null)
				{
					return "-1";
				}
				retStr=PackageUtil.byte2hex(response);
				log.debug("buf:"+retStr);
			}
		}
		try {
			wf.log("关闭socket连接.","调试");
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retStr;
	}
	
//	public static void main(String[] args) throws Exception {
//		String Msg="003C600018000060000000000008000020000000C000120000265A4632333030333533303331313033343531313233303100110000000100100003303031";
//		byte[] asc_buf=PackageUtil.hexStringToByte(Msg);
//		//SendSocket ssk=new SendSocket();
//		SocketToBoss ssk=new SocketToBoss();
//		String MsgData=ssk.detection(asc_buf);
//		log.debug("通信检测返回:"+MsgData);
//		Package64.UnPackage(PackageUtil.hexStringToByte(MsgData),"39,55,61",SocketToBoss.wf);
//	}
}

